package com.gzhryc.shared_device.oem.h5.mng.controllers.cdcz;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.models.SlotLock;
import com.gzhryc.shared_device.cdcz.code.dao.db.ChargeSocket;
import com.gzhryc.shared_device.oem.code.device.CDCZCacheManager;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 充电插座设备缓存
 */
public class ChargeSocketCacheController {

    @Route(value = { "/cdcz/device/cache/list.html" })
    public String list(HttpServletRequest request) {
        return "/cdcz/device/cache/list.html";
    }

    @Route(value = "/cdcz/device/cache/list.data")
    public ListResult<ChargeSocket> listData(HttpServletRequest request) {
        ListResult<ChargeSocket> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        List<ChargeSocket> data = CDCZCacheManager.self().findCDCZDevice();
        if(data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/cdcz/device/cache/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败");
        String deviceSn = WebUtils.getString(request, "deviceSn");
        if (StringTools.isNotBlank(deviceSn)) {
            try {
                CDCZCacheManager.self().removeChargeSocket(deviceSn);
                result.setCode(DtoConstants.SUCCESS, null);
            }catch (Exception e){
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = "/cdcz/device/cache/slot_lock/list.data")
    public ListResult<SlotLock> slotLockListData(HttpServletRequest request) {
        ListResult<SlotLock> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        List<SlotLock> data = CDCZCacheManager.self().getChargeSocketSlotLock();
        if (data != null && data.size() > 0) {
            result.setCode(DtoConstants.SUCCESS, null);
            result.getBody().setData(data);
        }
        return result;
    }

    @Route(value = { "/cdcz/device/cache/slot_lock/del.do" })
    public AjaxResult doDelLock(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除卡槽锁失败");
        String deviceSn = WebUtils.getString(request,"deviceSn");
        Integer slotNum = WebUtils.getInteger(request,"slotNum");
        if(StringTools.isNotBlank(deviceSn) && slotNum != null) {
            if (CDCZCacheManager.self().unlockCDCZSlot(deviceSn,slotNum)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }
}
